﻿Public Class FormReceive
    Dim objCA As New clsCPLHCK
    Dim objProdInfo As New clsProdInfo
    Dim dtCk As New DataTable
    Dim prod_ids As String
    Public usr As String
    Private Sub FormReceive_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        initDt()
    End Sub

    Public Sub initDt()
        dtCk.Columns.Add("sys_id")
        dtCk.Columns.Add("wh_no")
        dtCk.Columns.Add("prod_id")
        dtCk.Columns.Add("b_pgd_mes_sys_id")
        dtCk.Columns.Add("out_time")
        dtCk.Columns.Add("out_ope")
        dtCk.Columns.Add("type_value")
        dtCk.Columns.Add("hour_length")
    End Sub

    Private Sub txtProdId_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProdId.KeyPress
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) = False Then
            If e.KeyChar = vbCr Then
                Dim dt As DataTable = objCA.getListByProdId(txtProdId.Text.Trim(), "1")
                If dt.Rows.Count = 1 Then
                    Dim dr As DataRow() = dtCk.Select("sys_id='" & dt.Rows(0)("sys_id") & "'")
                    If dr.Length = 0 Then
                        If dt.Rows(0)("hour_length").Equals(DBNull.Value) Then
                            dtCk.Rows.Add(dt.Rows(0)("sys_id"), dt.Rows(0)("wh_no"), dt.Rows(0)("prod_id"),
                                        dt.Rows(0)("b_pgd_mes_sys_id"), dt.Rows(0)("into_time"), dt.Rows(0)("into_ope"), dt.Rows(0)("type_value"), String.Empty)
                        Else
                            dtCk.Rows.Add(dt.Rows(0)("sys_id"), dt.Rows(0)("wh_no"), dt.Rows(0)("prod_id"),
                                        dt.Rows(0)("b_pgd_mes_sys_id"), dt.Rows(0)("into_time"), dt.Rows(0)("into_ope"), dt.Rows(0)("type_value"), dt.Rows(0)("hour_length"))
                        End If
                        prod_ids &= "'" & dt.Rows(0)("prod_id") & "',"
                        dgv.AutoGenerateColumns = False
                        dgv.DataSource = dtCk
                        txtProdId.Text = String.Empty
                        txtProdId.Focus()
                    Else
                        MsgBox("该标签号已在表格中存在！", vbYes, "提示")
                    End If
                Else
                    If dt.Rows.Count = 0 Then
                        MsgBox("未查询到此生产编号出库信息", vbYes, "提示")
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub btnReceive_Click(sender As Object, e As EventArgs) Handles btnReceive.Click
        If dtCk.Rows.Count > 0 Then
            If MsgBox("确认接收吗？", vbYesNo, "提示") = vbYes Then
                If prod_ids.Length > 0 Then
                    prod_ids = prod_ids.Substring(0, prod_ids.Length - 1)
                End If
                If objProdInfo.updateProdInfo(prod_ids, usr) = -2 Then
                    MsgBox("接收失败", vbYes, "提示")
                Else
                    MsgBox("接收成功", vbYes, "提示")
                    dtCk.Rows.Clear()
                    txtProdId.Focus()
                End If
            End If
        Else
            MsgBox("请扫描或输入标签号，进行接收", vbYes, "提示")
        End If
    End Sub

    Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
        If dgv.SelectedRows.Count > 0 Then
            dtCk.Rows.RemoveAt(dgv.CurrentRow.Index)
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dtCk
        End If
    End Sub
End Class